package com.zsd.android.webrtcdemo.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class ScreenUtil {

    //屏幕的总宽度
    private static int mScreenWidth;

    private static void initScreenWidth (Context mContext) {
        if (mScreenWidth == 0) {
            WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();
            if (manager != null) {
                manager.getDefaultDisplay().getMetrics(dm);
                mScreenWidth = dm.widthPixels;
            }
        }
    }

    public static int getWidth(Context mContext, int size) {
        initScreenWidth(mContext);
        //只有4个人 及其以下
        if (size <= 4) {
            return mScreenWidth / 2;
        } else {
            return mScreenWidth / 3;
        }
    }

    public static int getX(Context mContext, int size, int index) {
        initScreenWidth(mContext);
        if (size <= 4) {
            if (size == 3 && index == 2) {
                //当会议室只有3个人的时候，第三个人的X值偏移量
                return mScreenWidth / 4;
            }
            return (index % 2) * mScreenWidth / 2;
        } else if (size <= 9) {
            if (size == 5) {
                //会议室里面有5个人
                if (index == 3) {
                    //第三个人的X值偏移量
                    return mScreenWidth / 6;
                }
                if (index == 4) {
                    //第四个人的X值偏移量
                    return mScreenWidth / 2;
                }
            }
            if (size == 7 && index == 6) {
                return mScreenWidth / 3;
            }
            if (size == 8) {
                if (index == 6) {
                    return mScreenWidth / 6;
                }
                if (index == 7) {
                    return mScreenWidth / 2;
                }
            }
            return (index % 3) * mScreenWidth / 3;
        }
        return 0;
    }

    public static  int getY(Context context,int size, int index) {
        initScreenWidth(context);
        if (size < 3) {
            return mScreenWidth / 4;
        } else if (size < 5) {
            if (index < 2) {
                return 0;
            } else {
                return mScreenWidth / 2;
            }
        } else if (size < 7) {
            if (index < 3) {
                return mScreenWidth / 2 - (mScreenWidth / 3);
            } else {
                return mScreenWidth / 2;
            }
        } else if (size <= 9) {
            if (index < 3) {
                return 0;
            } else if (index < 6) {
                return mScreenWidth / 3;
            } else {
                return mScreenWidth / 3 * 2;
            }

        }
        return 0;
    }

}
